﻿using AutoMapper;
using Org.BouncyCastle.Asn1.Mozilla;
using Serilog;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using WCS_Entity.Dto.UserDto;
using WCS_Entity.User;
using WCS_Repository.Interface;
using WCS_Repository.Repositories;
using WCS_Service.Interface;

namespace WCS_Service.Services
{
    /// <summary>
    /// 权限菜单管理服务
    /// </summary>
    public class MenuService : BaseService<MenusEntity>, IMenuService
    {
        private readonly IMenuRepository _menuRepository;
        private readonly IMapper _mapper;
        public MenuService(IMenuRepository menuRepository,
                           IMapper mapper) 
        {
            _menuRepository = menuRepository;
            _mapper = mapper;
        }

        public async Task<List<MenuDto>> QueryAllMenuAsync()
        {
            List<MenuDto> result = new List<MenuDto>();
            Expression<Func<MenusEntity, object>> orderEx = p => p.createTime;
            var menus = await _menuRepository.QueryPageAsync(null, orderEx, OrderByType.Desc, 1, 10);
            if (menus is null || !menus.Any())
            {
                Log.Error("查找所有菜单权限信息失败");
                return null;
            }
            foreach (var item in menus)
            {
                result.Add(_mapper.Map<MenuDto>(item));
            }
            return result;
        }

        public async Task<MenuDto> QueryMenuAsync(int menuId)
        {
            var menuInfo = await _menuRepository.QueryByIdAsync(menuId);
            if (menuInfo is null)
            {
                Log.Error($"查询菜单权限{menuId}不存在");
                return null;
            }
            var result = _mapper.Map<MenuDto>(menuInfo);
            return result;
        }
    }
}
